<template>
  <div class="picture-search-form">
    <!-- 搜索表单 -->
    <a-form layout="inline" :model="searchParams" @finish="doSearch">
      <a-form-item label="关键词" name="searchText">
        <a-input
          v-model:value="searchParams.searchText"
          placeholder="从名称和简介搜索"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="分类" name="category">
        <a-auto-complete
          v-model:value="searchParams.category"
          style="min-width: 180px"
          :options="categoryOptions"
          placeholder="请输入分类"
          allowClear
        />
      </a-form-item>
      <a-form-item label="标签" name="tags">
        <a-select
          v-model:value="searchParams.tags"
          style="min-width: 180px"
          :options="tagOptions"
          mode="tags"
          placeholder="请输入标签"
          allowClear
        />
      </a-form-item>
      <a-form-item label="日期" name="">
        <a-range-picker
          style="width: 400px"
          show-time
          v-model:value="dateRange"
          :placeholder="['请选择开始日期', '请选择结束时间']"
          format="YYYY/MM/DD HH:mm:ss"
          :presets="rangePresets"
          @change="onRangeChange"
        />
      </a-form-item>
      <a-form-item label="名称" name="name">
        <a-input v-model:value="searchParams.name" placeholder="请输入名称" allow-clear />
      </a-form-item>
      <a-form-item label="简介" name="introduction">
        <a-input v-model:value="searchParams.introduction" placeholder="请输入简介" allow-clear />
      </a-form-item>
      <a-form-item label="宽度" name="picWidth">
        <a-input-number v-model:value="searchParams.picWidth" />
      </a-form-item>
      <a-form-item label="高度" name="picHeight">
        <a-input-number v-model:value="searchParams.picHeight" />
      </a-form-item>
      <a-form-item label="格式" name="picFormat">
        <a-input v-model:value="searchParams.picFormat" placeholder="请输入格式" allow-clear />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit" style="width: 98px">搜索</a-button>
        <a-button style="margin-left: 8px; width: 98px" @click="doClear">重置</a-button>
      </a-form-item>
      <a-form-item label="按颜色搜索" style="margin-top: 16px">
        <ColorPicker format="hex" @change="onColorChange" />
      </a-form-item>
    </a-form>
  </div>
</template>

<script lang="ts" setup>
import { getTagCategoryUsingGet, searchPictureByColorUsingPost } from '@/api/pictureController'
import { message } from 'ant-design-vue'
import dayjs from 'dayjs'
import { onMounted, reactive, ref } from 'vue'
import { ColorPicker } from 'vue3-colorpicker'
import 'vue3-colorpicker/style.css'

interface Props {
  onSearch: (params: API.PictureQueryRequest) => void
  id: number | string
}

const props = withDefaults(defineProps<Props>(), {
  onSearch: () => {},
})

const searchParams = reactive<API.PictureQueryRequest>({})
const dateRange = ref<[]>([])

const doSearch = () => {
  props.onSearch(searchParams)
}

const onRangeChange = (dates: any[], dateStrings: string[]) => {
  if (dates.length < 2) {
    searchParams.startTime = undefined
    searchParams.endTime = undefined
  } else {
    searchParams.startTime = dates[0].toDate()
    searchParams.endTime = dates[1].toDate()
  }
}
const rangePresets = ref([
  {
    label: '过去7天',
    value: [dayjs().add(-7, 'day'), dayjs()],
  },
  {
    label: '过去14天',
    value: [dayjs().add(-14, 'day'), dayjs()],
  },
  {
    label: '过去30天',
    value: [dayjs().add(-30, 'day'), dayjs()],
  },
  {
    label: '过去90天',
    value: [dayjs().add(-90, 'day'), dayjs()],
  },
])

const categoryOptions = ref<string[]>([])
const tagOptions = ref<string[]>([])
const getTagCategoryOptions = async () => {
  const res = await getTagCategoryUsingGet()
  console.log('标签和分类', res)
  if (res.data.code === 0 && res.data.data) {
    //转换成下拉框接受的数据格式
    tagOptions.value = (res.data.data.tagList ?? []).map((data: string) => {
      return {
        value: data,
        label: data,
      }
    })
    categoryOptions.value = (res.data.data.categoryList ?? []).map((data: string) => {
      return {
        value: data,
        label: data,
      }
    })
  } else {
    message.error('加载失败' + res.data.message)
  }
}

//重置
const doClear = () => {
  //取消所有对象的值
  Object.keys(searchParams).forEach((key) => {
    searchParams[key] = undefined
  })
  dateRange.value = []
  props.onSearch?.(searchParams)
}

//按照颜色搜索
const onColorChange = async (color: string) => {
  const res = await searchPictureByColorUsingPost({
    picColor: color,
    spaceId: props.id,
  })
  if (res.data.code === 0 && res.data.data) {
    const data = res.data.data ?? []
    // dataList.value = data
    // total.value = data.length
  } else {
    message.error('按颜色搜索失败' + res.data.message)
  }
}

onMounted(() => {
  getTagCategoryOptions()
})
</script>

<style lang="scss">
.picture-search-form {
  margin-bottom: 20px;
  .ant-form-item {
    margin-top: 16px;
  }
}
</style>
